package cn.hs.ph.servlet;

import cn.hs.ph.dao.StoreDao;
import cn.hs.ph.dao.impl.StoreDaoImpl;
import cn.hs.ph.entity.Store;
import cn.hs.ph.util.JDBCDruidUtil;
import cn.hs.ph.util.Result;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/store")
public class StoreServlet extends HttpServlet {
    static int updateTotal=0;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        StoreDao storeDao = new StoreDaoImpl();
        String action = req.getParameter("action");
        if("selectAll".equals(action)){
            String pageNum = req.getParameter("pageNum");
            String pageSize = req.getParameter("pageSize");
            String phone = req.getParameter("phone");
            String keyword = req.getParameter("keyword");
            String shopStatus = req.getParameter("shopStatus");
            String startTime = req.getParameter("startTime");
            String endTime = req.getParameter("endTime");
            Map map = new HashMap();
            map.put("pageNum", pageNum);
            map.put("pageSize", pageSize);
            map.put("phone", phone);
            map.put("keyword", keyword);
            map.put("shopStatus", shopStatus);
            map.put("startTime", startTime);
            map.put("endTime", endTime);
            List<Store>list=storeDao.selectAll(map);
            int total = storeDao.selectCount(map);
            Map resultMap = new HashMap();
            resultMap.put("list", list);
            resultMap.put("total", total);
            Result result =Result.success(resultMap);
            resp.getWriter().write(JSON.toJSONString(result));
        }else if("addShop".equals(action)){
            String ShopName = req.getParameter("shopName");
            String ShopImage = req.getParameter("shopImage");
            String ShopPhone = req.getParameter("shopPhone");
            String ShopProvince = req.getParameter("shopProvince");
            String ShopCity = req.getParameter("shopCity");
            String ShopDistrict = req.getParameter("shopDistrict");
            String ShopAddress = req.getParameter("shopAddress");
            String ShopLongitude = req.getParameter("shopLongitude");
            String ShopLatitude = req.getParameter("shopLatitude");
            String ShopDescribe = req.getParameter ("shopDescribe");
            Store store = new Store(ShopName,ShopImage,ShopDescribe,ShopPhone,ShopProvince, ShopCity,  ShopDistrict,  ShopAddress,  ShopLongitude,  ShopLatitude);
            int num = storeDao.insert(store);
            if(num!=0){
                Result success = Result.success("新增成功");
                resp.getWriter().write(JSON.toJSONString(success));
            }else {
                Result success= Result.success("新增失败");
                resp.getWriter().write(JSON.toJSONString(success));
            }

        }else if("modifyShop".equals(action)){
            String Id = req.getParameter("shopId");
            int ShopId =0;
            if(Id!=null&&!"".equals(Id)){
                ShopId = Integer.parseInt(Id);
            }
            String ShopName = req.getParameter("shopName");
            String ShopImage = req.getParameter("shopImage");
            String ShopPhone = req.getParameter("shopPhone");
            String ShopProvince = req.getParameter("shopProvince");
            String ShopCity = req.getParameter("shopCity");
            String ShopDistrict = req.getParameter("shopDistrict");
            String ShopAddress = req.getParameter("shopAddress");
            String ShopLongitude = req.getParameter("shopLongitude");
            String ShopLatitude = req.getParameter("shopLatitude");
            String ShopDescribe = req.getParameter ("shopDescribe");
            Store store = new Store(ShopId,ShopName,ShopImage,ShopDescribe,ShopPhone,ShopProvince, ShopCity, ShopDistrict, ShopAddress,  ShopLongitude,  ShopLatitude);
            int num = new StoreDaoImpl().update(store);
            if(num!=0){
                Result success = Result.success("修改成功");
                resp.getWriter().write(JSON.toJSONString(success));
            }else {
                Result success= Result.success("修改失败");
                resp.getWriter().write(JSON.toJSONString(success));
            }

        }else if ("lookStore".equals(action)){
            String pageNum = req.getParameter("pageNum");
            String pageSize = req.getParameter("pageSize");
            String shopName = req.getParameter("shopName");
            String communityId = req.getParameter("communityId");

            Map map = new HashMap();
            map.put("pageNum", pageNum);
            map.put("pageSize", pageSize);
            map.put("shopName", shopName);
            map.put("communityId", communityId);

            List list = new StoreDaoImpl().lookStore(map);
            updateTotal = new StoreDaoImpl().lookStoreCount(map);

            Map resultMap = new HashMap();
            resultMap.put("list",list);
            resultMap.put("total",updateTotal);
            Result result = Result.success(resultMap);

            resp.getWriter().write(JSON.toJSONString(result));


        }else if ("addStore".equals(action)){
            String shopCommunityList = req.getParameter("ShopCommunityList");
            String[] sid = shopCommunityList.split(",");
            int shopId[]=new int[sid.length];
            if (sid!=null){
                for (int i=0;i<shopId.length;i++){
                    shopId[i]=Integer.parseInt(sid[i]);
                }
            }
            int communityId = Integer.parseInt(req.getParameter("communityId"));
            int num = new StoreDaoImpl().addStore(shopId, communityId);
            String sql = "update community set communityShopCount = ? where communityId = ?";
            Object obj[]={
                    updateTotal+sid.length,
                    communityId
            };
            JDBCDruidUtil.update(sql,obj);
            if(num!=0){
                Result success = Result.success("新增成功");
                resp.getWriter().write(JSON.toJSONString(success));
            }else {
                Result success= Result.success("新增失败");
                resp.getWriter().write(JSON.toJSONString(success));
            }
        }else if ("lookOther".equals(action)){
            String pageNum = req.getParameter("pageNum");
            String pageSize = req.getParameter("pageSize");

            Map map = new HashMap();
            map.put("pageNum", pageNum);
            map.put("pageSize", pageSize);

            List list = new StoreDaoImpl().lookStore(map);
            int total = new StoreDaoImpl().lookStoreCount(map);

            Map resultMap = new HashMap();
            resultMap.put("list",list);
            resultMap.put("total",total);
            Result result = Result.success(resultMap);

            resp.getWriter().write(JSON.toJSONString(result));

        } if ("associatedStoresRemove".equals(action)){
            String panghuShopId = req.getParameter("panghuShopId");
            String sql2 = "UPDATE shop SET communityId  = null WHERE shopId = ?";

            int shopID = 0;
            if (panghuShopId!=null&&!"".equals(panghuShopId)){
                shopID = Integer.parseInt(panghuShopId);
            }
            System.out.println(shopID);
            Object obj2[] = {shopID};
            JDBCDruidUtil.update(sql2, obj2);


            String communityId = req.getParameter("panghuCommunityId");
            int communityid = 0;
            if (communityId!=null&&!"".equals(communityId)){
                communityid = Integer.parseInt(communityId);
            }
//            删除
            String sql = "UPDATE community SET communityShopCount  = null WHERE communityId = ?";

            Object obj[] = {communityid};
//            System.out.println(communityid);
            int update = JDBCDruidUtil.update(sql, obj);
            String sql1 = "update community set communityShopCount = ? where communityId = ?";
            Object obj1[]={
                    updateTotal-1,
                    communityId
            };
            JDBCDruidUtil.update(sql1,obj1);
            if (update!=0){
                Result success = Result.success("删除关联门店成功");
                resp.getWriter().write(JSON.toJSONString(success));
            }else {
                Result error = Result.error("删除关联门店失败");
                resp.getWriter().write(JSON.toJSONString(error));
            }
        }


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
